﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueAttachmentTest
    /// </summary>
    [TestClass]
    public class IssueAttachmentTest
    {
        
  
        [TestMethod]
        public void TestAddIssueAttachment()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.IssueAttachment entity = new IssueTrackerService.IssueAttachment();

            client.Open();

            List<IssueTrackerService.Issue> issueList;

            try
            {

                issueList = client.ListIssues().ToList();

                if (issueList != null)
                {
                    entity.Id = System.Guid.NewGuid();
                    entity.Attachment = GetFileFromResource("sample");
                    entity.ContentType = "mime/word";
                    entity.CreatorId = "testuser";
                    entity.Filename = "sample.docx";
                    entity.IssueId = issueList.First().Id;

                    IssueTrackerService.IssueAttachment result = client.CreateIssueAttachment(entity);

                    Assert.IsNotNull(result.Id);
                }
                else
                {
                    Assert.Fail("Issue list is empty.");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestListByRef()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            IssueTrackerService.IssueAttachment entity = new IssueTrackerService.IssueAttachment();

            client.Open();

            List<IssueTrackerService.Issue> issueList;

            try
            {

                issueList = client.ListIssues().ToList();

                if (issueList != null)
                {
                    List<IssueAttachment> attachements =client.ListIssueAttachmentsByRef(issueList.First().Id).ToList();

                    Assert.IsNotNull(attachements);

                    Assert.IsTrue(attachements.Count > 0);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }

        }

        [TestMethod]
        public void TestUpdateIssueAttachment()
        {
            
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            List<IssueTrackerService.IssueAttachment> list;

            try
            {
                list = client.ListIssueAttachments().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueAttachment entity = list.First();

                    entity.Filename = "modfied.docx";

                    IssueTrackerService.IssueAttachment result = client.UpdateIssueAttachment(entity);

                    Assert.AreEqual("modfied.docx", entity.Filename);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
            
            
        }

        [TestMethod]
        public void TestDeleteIssueAttachment()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            System.Guid id;
            List<IssueTrackerService.IssueAttachment> list;

            try
            {
                list = client.ListIssueAttachments().ToList();

                if (list != null)
                {
                    IssueTrackerService.IssueAttachment entity = list.First();

                    id = entity.Id;

                    client.DeleteIssueAttachment(entity);

                    var checkIssueAttachment = client.GetIssueAttachment(id);

                    Assert.IsNull(checkIssueAttachment);
                }
                else
                {
                    Assert.Fail("IssueAttachment Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        private IssueTrackerService.Binary GetFileFromResource(string resourceName)
        {
            Byte[] thisFile = (Byte[])IssueTrackerTest.Properties.Resources.ResourceManager.GetObject(resourceName);

            IssueTrackerService.Binary returnBin = new IssueTrackerTest.IssueTrackerService.Binary();

            returnBin.Bytes = thisFile;

            return returnBin;

        }

        //private static string BinaryToString(System.Data.Linq.Binary binary)
        //{
        //    byte[] binarybytes = binary.ToArray();
        //    string result = "";
        //    foreach (byte b in binarybytes)
        //    {
        //        result += b.ToString() + "|";
        //    }
        //    result = result.Substring(0, result.Length - 1);

        //    return result;
        //}

        //private static System.Data.Linq.Binary StringToBinary(string s)
        //{
        //    string[] arr = s.Split('|');
        //    byte[] result = new byte[arr.Length];

        //    for (int i = 0; i < arr.Length; i++)
        //    {
        //        result[i] = Convert.ToByte(arr[i]);
        //    }
        //    return result;
        //}


    }
}
